﻿using SmallJob.Model;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Code;
using SmallShop.Services.Implements;
using SmallShop.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BackEnd.Controllers
{
     [Authorize]
    public class TestController : Controller
    {
        private readonly IEmailFormatService _EmailFormatService;
        private readonly IMessageService _MessageService;
        private readonly IOrderService _OrderService;
        private readonly IProvideService _ProvideService;
        private readonly ISkillService _SkillService;
        private readonly IUserService _UserService;
        private readonly IRequestService _RequestService;
        private readonly ITransactionService _TransactionService;
        public TestController(ITransactionService TransactionService, IUserService UserService, ISkillService SkillService, IMessageService MessageService, IProvideService ProvideService, IOrderService OrderService, IEmailFormatService EmailFormatService, IRequestService RequestService)
        {
            _UserService = UserService;
            _TransactionService = TransactionService;
            _SkillService = SkillService;
            _MessageService = MessageService;
            _ProvideService = ProvideService;
            _OrderService = OrderService;
            _EmailFormatService = EmailFormatService;
            _RequestService = RequestService;
        }


        public ActionResult Index()
        {
            return View();
        }
        private ToMongoDB database;
        public ActionResult ToMongoDB()
        {
            if (database == null) database = new ToMongoDB();
            //database.ClearCollection();
            //ProcessConvert();
            FixConversation();
            ViewData["cvs"] = database.GetListConversations();
            return View();
        }
        public void FixConversation()
        {
            database.FixConversation();
        }
        public void ProcessConvert()
        {
            var mes = _MessageService.All();
            var lsttype0 = new List<Message>();
            var lsttype1 = new List<Message>();
            var lsttype2 = new List<Message>();
            var lsttype3 = new List<Message>();
            var lsttype4 = new List<Message>();
            foreach (var item in mes)
            {
                if (item.Type == (int)MessageType.User_User)
                {
                    lsttype0.Add(item);
                }
                else if (item.Type == (int)MessageType.PROVIDE)
                {
                    lsttype1.Add(item);
                }
                else if (item.Type == (int)MessageType.REQUEST)
                {
                    lsttype2.Add(item);
                }
                else if (item.Type == (int)MessageType.ORDER_PROVIDE)
                {
                    lsttype3.Add(item);
                }
                else if (item.Type == (int)MessageType.ORDER_REQUEST)
                {
                    lsttype4.Add(item);
                }
            }
            FilterToSamePeople(lsttype0, (int)MessageType.User_User);
            FilterToSamePeople(lsttype1, (int)MessageType.PROVIDE);
            FilterToSamePeople(lsttype2, (int)MessageType.REQUEST);
            FilterToSamePeople(lsttype3, (int)MessageType.ORDER_PROVIDE);
            FilterToSamePeople(lsttype4, (int)MessageType.ORDER_REQUEST);
        }

        private void FilterToSamePeople(List<Message> mes, int type)
        {
            if (mes.Count > 0)
            {
                var curmes = mes[0];
                var people1id = curmes.SenderID;
                var people2id = curmes.ReceiveID;
                var lstsimilar = mes.Where(m => (m.SenderID == people1id && m.ReceiveID == people2id) || (m.SenderID == people2id && m.ReceiveID == people1id)).ToList();
                foreach (var item in lstsimilar)
                {
                    mes.Remove(item);
                }
                FilterToSameConversation(lstsimilar, type);
                if (mes.Count > 0)
                {
                    FilterToSamePeople(mes, type);
                }
            }
        }
        private void FilterToSameConversation(List<Message> mes, int type)
        {
            if (mes.Count > 0)
            {
                var curmes = mes[0];
                var lstsimilar = new List<Message>();
                if (type == (int)MessageType.PROVIDE)
                {
                    var id = mes[0].ProvideID;
                    lstsimilar = mes.Where(m => m.ProvideID == id).ToList();
                }
                else if (type == (int)MessageType.REQUEST)
                {
                    var id = mes[0].RequestID;
                    lstsimilar = mes.Where(m => m.RequestID == id).ToList();
                }
                else
                {
                    var id = mes[0].OrderID;
                    lstsimilar = mes.Where(m => m.OrderID == id).ToList();
                }
                foreach (var item in lstsimilar)
                {
                    mes.Remove(item);
                }
                CreateConversationAndSave(lstsimilar, type);
                 if (mes.Count > 0)
                {
                    FilterToSameConversation(mes, type);
                }
            }
        }
        private void CreateConversationAndSave(List<Message> mes, int type)
        {
            MConversation conversation = new MConversation();
            conversation.Person1ID = mes[0].SenderID;
            conversation.Person2ID = mes[0].ReceiveID;
            conversation.Type = type;
            conversation.User1 = database.ConvertUser(_UserService.GetById(conversation.Person1ID));
            conversation.User2 = database.ConvertUser(_UserService.GetById(conversation.Person2ID));
            if (type == (int)MessageType.ORDER_PROVIDE || type == (int)MessageType.ORDER_REQUEST)
            {
                conversation.Order = database.ConvertOrder(_OrderService.GetById(mes[0].OrderID));
                if (type == (int)MessageType.ORDER_PROVIDE)
                {
                    conversation.Order.Provide = database.ConvertProvide(_ProvideService.GetById(mes[0].ProvideID));
                }
                else
                {
                    conversation.Order.Request = database.ConvertRequest(_RequestService.GetById(mes[0].RequestID));
                }
                conversation.ShortID = MessageHelper.BuildShortIDOrder(conversation.Order.OrderID);
            }
            else if (type == (int)MessageType.PROVIDE)
            {
                conversation.Provide = database.ConvertProvide(_ProvideService.GetById(mes[0].ProvideID));
                conversation.ShortID = MessageHelper.BuildShortIDProvide(conversation.Provide.ID, (conversation.Person1ID == conversation.Provide.HostID ? conversation.Person2ID : conversation.Person1ID));
            }
            else if (type == (int)MessageType.REQUEST)
            {
                conversation.Request = database.ConvertRequest(_RequestService.GetById(mes[0].RequestID));
                conversation.ShortID = MessageHelper.BuildShortIDRequest(conversation.Request.ID, (conversation.Person1ID == conversation.Request.HostID ? conversation.Person2ID : conversation.Person1ID));
            }
            else
            {
                conversation.ShortID = MessageHelper.BuildShortIDNormal(conversation.Person1ID, conversation.Person2ID);
            }
            var lst = database.ToMongoMessage(mes,conversation).OrderBy(m => m.Date).ToList();
            conversation.Message = lst.Last();
            database.AddMConversation(conversation);
        }
    }
}
